#ifndef QOBJECTPOOL_H
#define QOBJECTPOOL_H

#include <QHash>

template <class T>
class QObjectPool
{
public:         
    QObjectPool(quint8 maxPool);
    ~QObjectPool();
	
    T* acquire();
    void release(T*);
private:
	QHash<T*, bool> m_usable;
};

#include "qobjectpool.h"


template <class T>
QObjectPool<T>::QObjectPool(quint8 maxPool)
{
	for(int i=0;i<maxPool;i++)
		m_usable[new T()] = true;
}

template <class T>
QObjectPool<T>::~QObjectPool(void)
{
	QHash<T*,bool>::iterator it;
	for(it=m_usable.begin();it != m_usable.end();it++)
		delete it.key();
}

template <class T>
T*QObjectPool<T>::acquire()
{
	QHash<T*,bool>::iterator it;
	for(it=m_usable.begin();it != m_usable.end();it++)
	{
		if(it.value())
		{
			m_usable[it.key()] = false;
			return it.key();
		}
	}
	return NULL;
}

template <class T>
void QObjectPool<T>::release(T* release)
{
	if(m_usable.contains(release))
		m_usable[release] = true;
}

#endif

